package com.qunar.wan.web.controller;

import com.google.common.base.Preconditions;
import com.qunar.wan.common_api.model.Activity;
import com.qunar.wan.common_api.model.User;
import com.qunar.wan.common_api.model.UserActivity;
import com.qunar.wan.common_api.model.enums.EvaluateStatus;
import com.qunar.wan.common_api.service.UserActivityService;
import com.qunar.wan.web.controller.common.AbstractController;
import com.qunar.wan.common_api.serializer.RowBounds;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: huiming.suo Date: 13-11-28 Time: 下午3:23
 */
@Controller
@RequestMapping("userActivity")
public class UserActivityController extends AbstractController {

    @Resource
    private UserActivityService userActivityService;

    /**
     * 用户参加过的活动
     */
    @RequestMapping(value = "participated", method = RequestMethod.GET)
    @ResponseBody
    public Object selectParticipatedActsByUserId(@RequestParam(value = "id") int userId, @RequestParam int page,
            @RequestParam(defaultValue = "4") int perPageNum) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(userId);
        RowBounds rowBounds = new RowBounds((page - 1) * perPageNum, perPageNum);
        List<Activity> userActivities = userActivityService.selectParticipatedActsByUserId(userId, rowBounds);
        return dataJson(userActivities, System.currentTimeMillis() - start).put("pageNum",
                (userActivityService.selectParticipatedActsCount(userId) - 1) / perPageNum + 1);
    }

    /**
     * 用户报名参加的活动
     */
    @RequestMapping(value = "enrolled", method = RequestMethod.GET)
    @ResponseBody
    public Object selectEnrollActsByUserId(@RequestParam(value = "id") int userId,
            @RequestParam(defaultValue = "10") int perPageNum) {
        Preconditions.checkNotNull(userId);
        RowBounds rowBounds = new RowBounds(0, perPageNum);
        long start = System.currentTimeMillis();
        List<Activity> activities = userActivityService.selectEnrollActsByUserId(userId, rowBounds);
        return dataJson(activities, System.currentTimeMillis() - start);
    }

    /**
     * 参加活动的部分用户
     */
    @RequestMapping(value = "enrolledUser", method = RequestMethod.GET)
    @ResponseBody
    public Object selectUsersByActId(@RequestParam(value = "id") int actId, @RequestParam int page,
            @RequestParam(defaultValue = "6") int perPageNum) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(actId);
        RowBounds rowBounds = new RowBounds((page - 1) * perPageNum, perPageNum);
        List<User> enrolledUsers = userActivityService.selectUsersByActId(actId, rowBounds);
        return dataJson(enrolledUsers, System.currentTimeMillis() - start).put("pageNum",
                (userActivityService.getActPartyNum(actId) - 1) / perPageNum + 1);
    }

    /**
     * 参加活动的所有用户
     */
    @RequestMapping(value = "allEnrolledUser", method = RequestMethod.GET)
    @ResponseBody
    public Object selectUsersByActId(@RequestParam(value = "id") int actId) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(actId);
        List<User> allUsers = userActivityService.selectUsersByActId(actId);
        return dataJson(allUsers, System.currentTimeMillis() - start);
    }

    @RequestMapping(value = "isUserEnroll", method = RequestMethod.GET)
    @ResponseBody
    public Object isUserEnroll(@RequestParam(value = "actId") int actId, @RequestParam(value = "userId") int userId) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(actId);
        Preconditions.checkNotNull(userId);
        return dataJson(userActivityService.isUserEnroll(userId, actId), System.currentTimeMillis() - start);
    }

    @RequestMapping(value = "getUserEvaluateStatus", method = RequestMethod.GET)
    @ResponseBody
    public Object getUserEvaluateStatus(@RequestParam(value = "actId") int actId,
            @RequestParam(value = "userId") int userId) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(actId);
        Preconditions.checkNotNull(userId);
        return dataJson(userActivityService.getUserEvaluateStatus(userId, actId), System.currentTimeMillis() - start);
    }

    @RequestMapping(value = "updateUserEvaluateStatus", method = RequestMethod.POST)
    @ResponseBody
    public Object updateUserEvaluateStatus(@RequestParam(value = "actId") int actId,
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "evaluateStatus") EvaluateStatus evaluateStatus) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(actId);
        Preconditions.checkNotNull(userId);
        UserActivity ua = new UserActivity();
        ua.setUserId(userId);
        ua.setActId(actId);
        ua.setEvaluateStatus(evaluateStatus);
        return dataJson(userActivityService.updateObj(ua), System.currentTimeMillis() - start);
    }
}
